import { Injectable } from '@nestjs/common';
import { CreateStudentDto } from './dto/create-student.dto';
import { UpdateStudentDto } from './dto/update-student.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Student } from './entities/student.entity';
import { Repository } from 'typeorm';
import { StatusText } from 'src/types/enum';

@Injectable()
export class StudentService {

  constructor(@InjectRepository(Student) private studentRepository: Repository<Student>) { }

  async create(createStudentDto: CreateStudentDto) {
    const result = await this.studentRepository.save(createStudentDto);
    return {
      code: 200,
      msg: StatusText.CREATE_SUCCESS,
      data: result
    };
  }

  async findAll() {
    const result = await this.studentRepository.find()
    return {
      code: 200,
      msg: StatusText.SEARCH_SUCCESS,
      data: {
        list: result
      }
    };
  }

  async findOne(id: number) {
    const result = await this.studentRepository.findOne({
      where: {
        id
      }
    })
    return {
      code: 200,
      msg: StatusText.DETAIL_SUCCESS,
      data: result
    };
  }

  async update(id: number, updateStudentDto: UpdateStudentDto) {
    const result = await this.studentRepository.update(id, updateStudentDto);
    return {
      code: 200,
      msg: StatusText.UPDATE_SUCCESS,
      data: result
    };
  }

  async remove(id: number) {
    const result = await this.studentRepository.delete({
      id
    })
    return {
      code: 200,
      msg: StatusText.DELETE_SUCCESS,
      data: result
    };
  }

  async batchRemove(ids: number[]) {
    console.log(ids, "参数")
    const result = await this.studentRepository.delete(ids)
    return {
      code: 200,
      msg: StatusText.DELETE_SUCCESS,
      data: result
    };
  }
}
